package com.syaccp.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;;
import com.syaccp.model.TOrder;
import com.syaccp.model.TOrderDetail;
import com.syaccp.page.pageUtils;
import com.syaccp.request.TOrderMaterialRequest;
import com.syaccp.response.TOrderMaterialResponse;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.service.TOrderService;
import com.syaccp.utils.Result;
import com.syaccp.utils.imgUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("操作任务表")
@RestController
@CrossOrigin
@RequestMapping("/api/tOrderDetail/")
public class TOrderDetailController {

    @Resource
    TOrderService tOrderService;
    @Resource
    TOrderDetailService tOrderDetailService;

    @ApiOperation(value = "批量删除任务")
    @GetMapping("/delInfoByIds")
    public Result<Integer> delInfoByIds(@RequestParam List<Integer> ids){
        return Result.success(tOrderDetailService.delInfoByIds(ids));
    }
    @ApiOperation(value = "批量设为精选")
    @GetMapping("/updateSelections")
    public Result<Integer> updateSelections(@RequestParam List<Integer> ids){
        return Result.success(tOrderDetailService.updateSelection(ids));
    }

    /**
     * 多条件查询任务
     *
     * @return
     */
    @ApiOperation(value = "操作多条件查询订单表")
    @GetMapping("/queryInfoByManyCondition")
    public synchronized  Result<TOrderMaterialResponse> queryInfoByManyCondition(Integer number, @ModelAttribute TOrderMaterialRequest orderRequest) {
        TOrderMaterialResponse tOrderMaterialResponse = new TOrderMaterialResponse();
        Integer totalCount = tOrderDetailService.queryInfoByConditionsNumber(orderRequest);//查询总条数
        pageUtils page = new pageUtils();
        page.setPageSize(10);
        page.setTotalCount(totalCount);
        if (totalCount > 0) {
            if (number >= page.getTotalPageCount()) {
                page.setCurrPageNo(page.getTotalPageCount());
            } else if (number <= 0) {
                page.setCurrPageNo(1);
            } else {
                page.setCurrPageNo(number);
            }
            tOrderMaterialResponse.setAboutPageInfo(page);  //返回的分页信息
            List<TOrderDetail> tOrderDetailList = tOrderDetailService.queryInfoByConditions(orderRequest, page);//查询并分页
            List<TOrder> aboutOrderInfo=new ArrayList<>();  //相关订单集合
            List<TOrderDetail> detailListCollectTwo = tOrderDetailList.stream().peek(info -> {
                    //通过客户评价和风格特征查询订单
                        aboutOrderInfo.add(tOrderService.queryByOrderId(info.getOrderId()));
                        if (StringUtils.isBlank(info.getImage())) { //拼接图片前缀
                            info.setImage(imgUtils.DEFAULT_PIC);
                        } else {
                            info.setImage(imgUtils.setRealPicPath(info.getImage()));
                            if (!imgUtils.checkUrlConnection(info.getImage())) {
                                info.setImage(imgUtils.DEFAULT_PIC);
                            }
                        }
            }).collect(Collectors.toList());
            tOrderMaterialResponse.setTOrderDetailList(detailListCollectTwo);
            tOrderMaterialResponse.setAboutOrderDetailStyles(aboutOrderInfo);
        }
        return Result.success(tOrderMaterialResponse);
    }

    @ApiOperation(value = "查询审核级别")
    @GetMapping("/queryAllAboutOrderInfo")
    public Result<List<TOrderDetail>> queryAllAboutOrderInfo() {
        return Result.success(tOrderDetailService.queryAuditions());
    }
    @ApiOperation(value = "查询所属公司")
    @GetMapping("/queryAllAboutCompany")
    public Result<List<TOrderDetail>> queryAllAboutCompany() {
        return Result.success(tOrderDetailService.queryCompanyNames());
    }
    @ApiOperation(value = "查询模型师")
    @GetMapping("/queryAllAboutModel")
    public Result<List<TOrderDetail>> queryAllAboutModel() {
        return Result.success(tOrderDetailService.queryModelNames());
    }
}
